var Nodemailer = require('nodemailer'),
    Promise = require('bluebird'),
    mailConfig = require('../config/config'),
    mailConstants = require('../helpers/mail-constants'),
    util = require('../helpers/util');
var transporter = Nodemailer.createTransport([
    'smtps://',
    process.env.MAIL_SERVER_USER_ID,
    ':',
    process.env.MAIL_SERVER_USER_PASSWORD,
    '@smtp.gmail.com'
].join(''));

module.exports = {
    sendConfirmationMail: function (username, email, confirmationUrl) {
        var confirmationLink = process.env.APP_CLIENT_BASE_URL_PROD + confirmationUrl,
            mailBody = util.formMailBody('REGISTRATION_BODY', {
                username: username,
                confirmationLink: confirmationLink
            }),
            mailOptions = {
                from: process.env.MAIL_SERVER_USER_ID,
                to: email,
                subject: mailConstants.REGISTRATION_SUBJECT,
                html: mailBody
            };
        return new Promise(function (resolve, reject) {
            transporter.sendMail(mailOptions, function (error, info) {
                if (error) {
                    return reject(error);
                } else {
                    return resolve(info.response);
                }
            });
        });
    },
    sendPasswordResetMail: function (email, resetUrl, firstName) {
        var resetLink = process.env.APP_CLIENT_BASE_URL_PROD + resetUrl,
            mailBody = util.formMailBody('PASSWORD_RESET_BODY', {
                firstName: firstName,
                resetLink: resetLink
            }),
            mailOptions = {
                from: process.env.MAIL_SERVER_USER_ID,
                to: email,
                subject: mailConstants.PASSWORD_RESET_SUBJECT,
                html: mailBody
            };
        return new Promise(function (resolve, reject) {
            transporter.sendMail(mailOptions, function (error, info) {
                if (error) {
                    return reject(error);
                } else {
                    return resolve(info.response);
                }
            });
        });
    },
    sendAbuseReportedMail: function (firstName, lastName, reason, idea, iLink) {
        var ideaLink = process.env.APP_CLIENT_BASE_URL_PROD + iLink,
            mailBody = util.formMailBody('REPORT_ABUSE_BODY', {
                firstName: firstName,
                lastName: lastName,
                ideaReason: reason,
                ideaTitle: idea.title,
                ideaLink: ideaLink
            }),
            mailOptions = {
                from: process.env.MAIL_SERVER_USER_ID,
                to: process.env.ABUSE_REPORTING_MAIL,
                subject: mailConstants.REPORT_ABUSE_SUBJECT,
                html: mailBody
            };
        return new Promise(function (resolve, reject) {
            transporter.sendMail(mailOptions, function (error, info) {
                if (error) {
                    return reject(error);
                } else {
                    return resolve(info.response);
                }
            });
        });
    },
    sendAbuseApproveMail: function (iUser, aUser, idea, abuse) {
        var iEmail = iUser.email,
            aEmail = aUser.email,
            mailBody = util.formMailBody('IDEA_ABUSE_APPROVE_BODY', {
                firstName: iUser.firstName,
                abuseReason: abuse.reason,
                ideaTitle: idea.title
            }),
            mailOptions = {
                from: process.env.MAIL_SERVER_USER_ID,
                to: iEmail,
                subject: mailConstants.ABUSE_APPROVE_SUBJECT,
                html: mailBody
            },
            mailBody1 = util.formMailBody('ABUSE_APPROVE_BODY', {
                firstName: aUser.firstName,
                abuseReason: abuse.reason,
                ideaTitle: idea.title
            }),
            mailOptions1 = {
                from: process.env.MAIL_SERVER_USER_ID,
                to: aEmail,
                subject: mailConstants.ABUSE_APPROVE_SUBJECT,
                html: mailBody1
            };
        return new Promise(function (resolve, reject) {
            transporter.sendMail(mailOptions, function (error, info) {
                if (error) {
                    return reject(error);
                } else {
                    transporter.sendMail(mailOptions1, function (error, info) {
                        if (error) {
                            return reject(error);
                        } else {
                            return resolve(info.response);
                        }
                    });

                }
            });
        });
    },
    sendAbuseRejectMail: function (iUser, aUser, idea, abuse, adminReason) {
        var iEmail = iUser.email,
            aEmail = aUser.email,
            mailBody = util.formMailBody('IDEA_ABUSE_REJECT_BODY', {
                firstName: iUser.firstName,
                abuseReason: abuse.reason,
                ideaTitle: idea.title
            }),
            mailOptions = {
                from: process.env.MAIL_SERVER_USER_ID,
                to: iEmail,
                subject: mailConstants.ABUSE_REJECT_SUBJECT,
                html: mailBody
            },
            mailBody1 = util.formMailBody('ABUSE_REJECT_BODY', {
                firstName: aUser.firstName,
                abuseReason: abuse.reason,
                ideaTitle: idea.title,
                adminReason: adminReason
            }),
            mailOptions1 = {
                from: process.env.MAIL_SERVER_USER_ID,
                to: aEmail,
                subject: mailConstants.ABUSE_REJECT_SUBJECT,
                html: mailBody1
            };
        return new Promise(function (resolve, reject) {
            transporter.sendMail(mailOptions, function (error, info) {
                if (error) {
                    return reject(error);
                } else {
                    transporter.sendMail(mailOptions1, function (error, info) {
                        if (error) {
                            return reject(error);
                        } else {
                            return resolve(info.response);
                        }
                    });
                }
            });
        });
    },
    sendUpdateStatusMail: function (users) {
        var status;
        if (users.isActive) {
            status = "Active";
        } else {
            status = "Inactive";
        }
        mailBody = util.formMailBody('STATUS_UPDATE_BODY', {
            firstName: users.firstName,
            status: status
        }),
            mailOptions = {
                from: process.env.MAIL_SERVER_USER_ID,
                to: users.email,
                subject: mailConstants.STATUS_UPDATE_SUBJECT,
                html: mailBody
            };
        return new Promise(function (resolve, reject) {
            transporter.sendMail(mailOptions, function (error, info) {
                if (error) {
                    return reject(error);
                } else {
                    return resolve(info.response);
                }
            });
        });
    },
};